'use strict'

const Controller = require('egg').Controller

class HomeController extends Controller {
  //数据
  async linelist() {
    const { ctx } = this
    const list = await this.app.mysql.select('line')
    ctx.body = {
      code: 200,
      msg: '线路数据请求成功',
      list,
    }
  }

  //添加
  async lineadd() {
    const { ctx } = this
    const { linetype, linetrain, lineleng, linetime, falg } = ctx.request.body
    console.log(linetype, linetrain, lineleng, linetime)
    const res = await this.app.mysql.insert('line', {
      trains: `${linetrain}`,
      type: `${linetype}`,
      leng: `${lineleng}`,
      tacktime: `${linetime}`,
      falg: 'true',
    })
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('line')
      ctx.body = {
        code: 200,
        msg: '线路添加成功',
        list,
      }
    } else {
      ctx.body = {
        code: 400,
        msg: '线路添加失败',
      }
    }
  }

  //删除
  async linedel() {
    const { ctx } = this
    const { id } = ctx.query
    const res = await this.app.mysql.delete('line', { id })
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('line')
      ctx.body = {
        code: 200,
        msg: '删除成功',
        list,
      }
    } else {
      ctx.body = {
        code: 400,
        msg: '删除失败',
        list,
      }
    }
  }

  //编辑
  async lineedit() {
    const { ctx } = this
    const { id, trains, type, leng, tacktime } = ctx.request.body
    const row = {
      id,
      trains,
      type,
      leng,
      tacktime,
    }
    const res = await this.app.mysql.update('line', row)
    if (res.affectedRows === 1) {
      const list = await this.app.mysql.select('line')
      ctx.body = {
        code: 200,
        msg: '修改成功',
        list,
      }
    } else {
      ctx.body = {
        code: 400,
        msg: '修改成功',
      }
    }
  }
}

module.exports = HomeController
